<?php

namespace addons\Patrol\merchant\controllers;

use addons\Patrol\common\models\PatrolClasses;
use addons\Patrol\common\models\PatrolPlace;
use addons\Patrol\common\models\PatrolPlan;
use Yii;
use common\traits\Curd;
use common\models\base\SearchModel;
use addons\Patrol\merchant\controllers\BaseController;
use common\enums\StatusEnum;

class PlanController extends BaseController
{
    use Curd;

    /**
     * @var Plan
     */
    public $modelClass = PatrolPlan::class;


    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'relations' => ['member' => ['realname'],'place' => ['name'],'patrolClasses' => ['title']], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['member.realname','place.name','patrolClasses.title'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段

            'defaultOrder' => [
                'id' => SORT_DESC,
            ],
            'pageSize' => $this->pageSize
        ]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query->andFilterWhere([PatrolPlan::tableName().'.merchant_id' => $this->getMerchantId()]);
//        $dataProvider->query->addGroupBy(['member_id']);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,

        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())&&$model->save()) {

            $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' =>$model,
            'data' => Yii::$app->services->merchantMember->getMap(),
            'places' => PatrolPlace::getDropDown(),
            'classes' => PatrolClasses::getDropDown(),
        ]);
    }
    public function actionCreate()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            if(!$model->classes)
                return $this->message("请先设置班次", $this->redirect(Yii::$app->request->referrer), 'error');

            foreach ($model->attend as $member_id)
            {

                foreach ($model->classes as $class_id){
                    $classes = PatrolClasses::findOne($class_id);

                    $plan = clone $model;
                    $plan->place_id = $model->place_id;
                    $plan->classes_id = $class_id;
                    $plan->member_id = $member_id;
                    $plan->check_date = $model->check_date;
                    $plan->begin_time = $classes->begin_time;
                    $plan->end_time = $classes->end_time;
                    $plan->save();
                }

            }
            $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' =>$model,
            'data' => Yii::$app->services->merchantMember->getMap(),
            'places' => PatrolPlace::getDropDown(),
            'classes' => PatrolClasses::getDropDown(),

        ]);
    }
    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionSelect()
    {
        $model = new PatrolPlan();
        $model->check_date = Yii::$app->request->get('date', null);
        if ($model->load(Yii::$app->request->post())) {
            if(!$model->classes)
                return $this->message("请先设置班次", $this->redirect(Yii::$app->request->referrer), 'error');

            foreach ($model->attend as $member_id)
            {
                foreach ($model->classes as $class_id){
                    $classes = PatrolClasses::findOne($class_id);

                    $plan = clone $model;
                    $plan->place_id = $model->place_id;
                    $plan->classes_id = $class_id;
                    $plan->member_id = $member_id;
                    $plan->check_date = $model->check_date;
                    $plan->begin_time = $classes->begin_time;
                    $plan->end_time = $classes->end_time;
                    $plan->save();
                }

            }
            $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' =>$model,
            'data' => Yii::$app->services->merchantMember->getMap(),
            'places' => PatrolPlace::getDropDown(),
            'classes' => PatrolClasses::getDropDown(),

        ]);
    }
    //排班修改
    public function actionModify()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) ) {
            $model->save();
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,

        ]);
    }
    public function actionView($id)
    {
        if (empty($id) || empty(($model = PatrolPlan::find()->where(['id' => $id])->all()))) {
            return $this->message('找不到该记录', $this->redirect(['index']), 'error');
        }
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    public function actionMy()
    {
        $model = PatrolPlan::find()->where(['member_id' => Yii::$app->user->id])->all();

        return $this->render('my', [
            'model' => $model,
        ]);
    }

    public function actionChange(){
        $model = $this->findModel('');
        if ($model->load(Yii::$app->request->post()) ) {
            $model->save();
            return $this->redirect(['index']);
        }
        return $this->render('change', [
            'model' => $model,
            'data' => Yii::$app->services->merchantMember->getMap(),
            'places' => PatrolPlace::getDropDown(),
        ]);
    }
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = $this->modelClass::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {

            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }
}
